var fs = require("fs");
function writeFile(fs,filePath,fileName,data,callback){
	fs.open(filePath+fileName,"w",0644,function(err,fd){//open or create
		if(err) throw err;
		if ((typeof data).toString()==="object") {
			data = JSON.stringify(data);
		}else{
			data = data.toString();
		}
		fs.write(fd,data,0,"utf8",function(err){
			if(err) throw err;
			callback&&callback(fs,filePath,fileName,data);
			fs.closeSync(fd);
		})
	})
}
fs.readFile("./src/avalon.js",
	{
		encoding:"utf8",
		flag :"r"
	},
	function(err,fileData){
		if(err){
			throw err;
		}
		fileData = fileData.toString();
		var match = /\/[*]{3}[* \n]+([\w\W ]+?)[* \n]+\//g;
		var modules = fileData.split(match);
		var Length = modules.length;
		console.log(modules[1].replace(/[*]/g,"").trim());

		writeFile(fs,"./lib/","0_baseVariable.js",modules[0])

		for(var i = 1,j=0;i<Length;i+=2){
			var fileName = modules[i].replace(/[*]/g,"").trim()
			console.log(fileName);
			writeFile(fs,"./avalon_lib/",(++j).toString(36)+"_"+fileName+".js",modules[i+1]);
		}
	});